import json
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
from apps.common.ali import setting
from .serializer import *

request = CommonRequest()
request.set_accept_format('json')
request.set_domain('dysmsapi.aliyuncs.com')
request.set_method('POST')
request.set_protocol_type('https') # https | http
request.set_version('2017-05-25')
request.add_query_param('RegionId', "cn-hangzhou")



# 新增
def createValidateCode(params):
    serializer = ValidateCodeSerializer(data=params)
    if serializer.is_valid():
        serializer.save()
    else:
        print('error', serializer.error_messages)

# 更新
def updateValidateCode(params):
    model = ValidateCode.objects.filter(id = params.get("id")).first()
    if model:
        serializer = ValidateCodeSerializer(model, data = params, partial = True)
        if serializer.is_valid():
            serializer.save()
            return '更新成功'
        else:
            print('error', serializer.error_messages)
    else:
        return "记录不存在"

# 根据params查找
def findValidateCodeByParams(params):
    model=None
    if params.get('id'):
        try:
            model = ValidateCode.objects.get(id = params.get('id'))
            serializer = ValidateCodeSerializer(model)
            return serializer.data
        except ValidateCode.DoesNotExist:
            return "记录不存在"

def sendSmsForAli(params):
    print('send', params)
    request.set_action_name('SendSms')
    request.add_query_param('PhoneNumbers', params.get('phone'))
    request.add_query_param('SignName', setting.config.get(params.get('validateCodeCHN')).get('SignName'))
    request.add_query_param('TemplateCode', setting.config.get(params.get('validateCodeCHN')).get('TemplateCode'))
    request.add_query_param('TemplateParam', {"code": params.get('code')})
    response = None
    if params.get('validateCodeCHN') == 'xlsh':
        client = AcsClient(setting.config.get('xlsh').get('ACCESS_KEY_ID'), setting.config.get('xlsh').get('ACCESS_KEY_SECRET'), 'cn-hangzhou')
        response = client.do_action(request)
    elif params.get('validateCodeCHN') == 'qlw':
        client = AcsClient(setting.config.get('qlw').get('ACCESS_KEY_ID'), setting.config.get('qlw').get('ACCESS_KEY_SECRET'), 'cn-hangzhou')
        response = client.do_action(request)
    elif params.get('validateCodeCHN') == 'zjyh':
        client = AcsClient(setting.config.get('zjyh').get('ACCESS_KEY_ID'), setting.config.get('zjyh').get('ACCESS_KEY_SECRET'), 'cn-shenzhen')
        response = client.do_action(request)
    elif params.get('validateCodeCHN') == 'xlmp':
        client = AcsClient(setting.config.get('xlmp').get('ACCESS_KEY_ID'), setting.config.get('xlmp').get('ACCESS_KEY_SECRET'), 'cn-shenzhen')
        response = client.do_action(request)
    elif params.get('validateCodeCHN') == 'xlmp2':
        client = AcsClient(setting.config.get('xlmp2').get('ACCESS_KEY_ID'), setting.config.get('xlmp2').get('ACCESS_KEY_SECRET'), 'cn-shenzhen')
        response = client.do_action(request)
    else:
        print('validateCodeCHN未知')
    print(str(response, encoding = 'utf-8'))
    return json.loads(str(response, encoding = 'utf-8'))